package com.example.demoserver.controller;

import com.example.demoserver.entity.*;
import com.example.demoserver.service.*;

import com.example.demoserver.service.impl.PollingService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/train")
@RequiredArgsConstructor
@Component
public class ExamTrainquesController{

    private final PollingService pollingService;
    @Value("${examServer.ip}")
    private String ip;
    @Value("${examServer.port}")
    private String port;
    @Value("${inteUrl.dir}")
    private String dir;
    @Value("${app.name}")
    private String name;
    private final TrainQuestionsService trainQuestionsService;
    private final TrainQueoptionsService trainQueoptionsService;
    @GetMapping("/select")
    public List<TrainQuestions> select(String searchCode) {
        return trainQuestionsService.select(searchCode);
    }
    @GetMapping("/selectbycode")
    public Map<String, Object> selectbycode(String queCode) {
        List<TrainQuestions> list=trainQuestionsService.select(queCode);
        List<TrainQueDetail> list1=new ArrayList<>();
        for (TrainQuestions t:list) {
            List<TrainQueoptions> ts=trainQueoptionsService.selectByqueId(t.getQueid());
            TrainQueDetail trainQueDetail=new TrainQueDetail();
            trainQueDetail.setQuestions(t);
            trainQueDetail.setOptions(ts);
            list1.add(trainQueDetail);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("Code", "200");
        map.put("Msg", "成功");
        map.put("Data",list1);
        return map;
    }
    @GetMapping("/selectbyid")
    public Map<String, Object> selectById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        TrainQuestions trainQuestions=trainQuestionsService.selectById(id);
        if(trainQuestions!=null){
            map.put("Code", "200");
            map.put("Msg", "成功");
            map.put("Data",trainQuestions);
            return map;
        }
        map.put("Code", "500");
        map.put("Msg", "没有该试题");
        map.put("Data",null);
        return map;
    }
    @GetMapping("/traindetail")
    public Map<String, Object> trainDetail(Integer pageNumber,Integer pageSize) {
        PageInfo info=trainQuestionsService.selectPage(pageNumber,pageSize);
        List<TrainQuestions> list = info.getList();
        //List<ExamTrainques> list=new ArrayList<>();
        List<TrainQueDetail> l=new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            TrainQueDetail trainQueDetail=new TrainQueDetail();
            trainQueDetail.setQuestions(list.get(i));
            List<TrainQueoptions> list1= trainQueoptionsService.selectByqueId(list.get(i).getQueid());
            if(list1 !=null){
                trainQueDetail.setOptions(list1);
            }
            l.add(trainQueDetail);
        }
        Map<String, Object> map = new HashMap<>();
        if (list != null) {
            map.put("Code", "200");
            map.put("Msg", "成功");
        } else {
            map.put("Code", "500");
            map.put("Msg", "失败");
        }
        info.setList(l);
        map.put("Data", info);
        return map;
    }
    @GetMapping("/templateurl")
    public ResponseEntity<InputStreamResource> templateUrl(Integer id) throws FileNotFoundException {
        TrainQuestions e=trainQuestionsService.selectById(id);

        File file = new File(dir+"/"+e.getPreclass()+".inet"); // 设置你的文件路径
        if (!file.exists()) {
            return ResponseEntity.notFound().build(); // 如果文件不存在，返回404
        }
        InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

        return ResponseEntity.ok()
                // Content-Disposition
                .header("Content-Disposition", "attachment;filename=" + file.getName())
                // Content-Type
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                // Contet-Length
                .contentLength(file.length())
                .body(resource);

    }




}
